package com.ldx.pool;




import java.util.ArrayList;
import java.util.concurrent.*;

/**
 * @author lidexin
 * @create 2021-10-16 15:12
 */
//Excuters 工具类：3大方法
public class Demo01 {
    public static void main(String[] args) {
//         ExecutorService threadPool = Executors.newSingleThreadExecutor();
//         ExecutorService threadPool = Executors.newFixedThreadPool(5);
//         ExecutorService threadPool = Executors.newCachedThreadPool();
         ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                 2,
                 5,
                 3,
                 TimeUnit.SECONDS,
                 new LinkedBlockingDeque<>(3),
                 Executors.defaultThreadFactory(),
                 //银行满了，还有人进来，不处理这个人，抛出异常
//                 new ThreadPoolExecutor.AbortPolicy()
                 // 哪来的去哪里
//                   new ThreadPoolExecutor.CallerRunsPolicy()
                 //队列满了，丢掉任务，不会抛出异常
//                 new ThreadPoolExecutor.DiscardPolicy()
                 //队列满了，尝试去和最早的竞争，也不会抛出异常
                 new ThreadPoolExecutor.DiscardOldestPolicy()
                 );
        try {
            //最大承载：Deque + max
            //java.util.concurrent.RejectedExecutionException
            for (int i = 1; i <= 9; i++) {
                //使用了线程池之后，使用线程池来创建线程
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "ok");
                });
            }
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            //线程池用完，程序结束，关闭线程池
            threadPool.shutdown();
        }
    }
}
